package cn.itcast.usermanage.service;


import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.abel533.entity.Example;

import cn.itcast.usermanage.mapper.UserMapper;
import cn.itcast.usermanage.pojo.User;

@Service
public class UserService {

    @Autowired
    private UserMapper usermapper;

    public Boolean saveUser(User user) {
        // TODO Auto-generated method stub
        return this.usermapper.insert(user)==1;
    }

    public String doLogin(String username, String password) {
        // TODO Auto-generated method stub
        
        User record = new User();
        record.setUsername(username);
        record.setPassword(password);
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("username", record.getUsername());
        List<User> list = this.usermapper.selectByExample(example);
        
        User user = list.get(0);
        if(null == user){
            return null;
        }
        if(!StringUtils.equals(password, user.getPassword())){
            return null;
        }    
        String token =username;
        return token;
    }

    public Boolean saveUser(String username, String password) {
        // TODO Auto-generated method stub
        System.out.println(username);
        System.out.println(password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        
        return this.usermapper.insert(user)==1;
    }
    
    
}
